﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using DBAccess;
using System.Collections.Generic;
using System.Text;

namespace SD.Database
{
    public class FleetsFunctions
    {
        public static void SentFleet(int Owner, int fromSys, int fromPlanet, int toSys, int toPlanet, int cs, int tr, int ds, int cr, int bs)
        {
            int energy = (int)UserFunctions.GetPlayerScience(Owner, 3);
            int speed = UserFunctions.GetPlayerInt(Owner, "Speed");
            List<int> s1 = Systems.SystemCoordinates(fromSys);
            List<int> s2 = Systems.SystemCoordinates(toSys);
            int planetOwner = Planets.PlanetOwner(toSys, toPlanet);
            int same;
            if (Owner == planetOwner) same = 1; else same = 0;
            double tt = Equations.TravelTime(s1[0], s2[0], s1[1], s2[1], fromPlanet, toPlanet, same, energy, speed);
            int h = (int)tt;
            int m = (int)(tt - h * 1.0);
            int s = (int)(tt - h * 1.0 - m * 1.0);
            TimeSpan spa = new TimeSpan(h, m, s);
            DateTime arrivialTime = DateTime.Now.Add(spa);
            if (planetOwner > 1 && Owner != planetOwner)
            {
                StringBuilder sb = new StringBuilder("Incoming fleet of player: <a href=\"PlayerInfo.aspx?pId=" + Owner.ToString() + "\">" + UserFunctions.PlayerName(Owner) + "</a><br />");
                if (tr > 0) sb.Append("\tTransports: " + tr.ToString() + "<br />");
                if (ds > 0) sb.Append("\tDestroyers: " + ds.ToString() + "<br />");
                if (cr > 0) sb.Append("\tCruisers: " + cr.ToString() + "<br />");
                if (bs > 0) sb.Append("\tBattleships: " + bs.ToString() + "<br />");
                sb.Append("Will arrive at: " + arrivialTime.ToString()).Append("<br />To planet: ");
                sb.Append(Systems.SystemName(toSys)).Append(" #").Append(toPlanet.ToString());
                StoredProcedures.InsertNews(planetOwner, arrivialTime, sb.ToString());
            }

            DAL.GetInstance().InsertData("Insert Into Fleets(OwnerId, ColonyShips, Transports, Destroyers, Crusiers, Battleships, FromSystem, FromPlanet, ToSystem, ToPlanet, ArrivialTime) Values (@param0, @param1, @param2, @param3, @param4, @param5, @param6, @param7, @param8, @param9, @param10)",
                new List<object>() { Owner, cs, tr, ds, cr, bs, fromSys, fromPlanet, toSys, toPlanet, arrivialTime });
            DAL.GetInstance().UpdateData("Update planets SET ColonyShips = ColonyShips-@param0, Transports = Transports-@param1, Destroyers=Destroyers-@param2, Crusiers=Crusiers-@param3, Battleships=Battleships-@param4 Where PlanetID = @param5 and SystemId = @param6",
                new List<object>() { cs, tr, ds, cr, bs, fromPlanet, fromSys });
            Planets.CheckPlanet(fromSys, fromPlanet);
        }

        public static void RemoveFleet(int fid)
        {
            DAL.GetInstance().DeleteData("Delete from Fleets Where FleetID = @param0", new List<object>() { fid });
        }
        public static bool FleetExists(int systemId, int planetId)
        {
            DataSet ds = DAL.GetInstance().GetData("SELECT ColonyShips, Transports, Destroyers, Crusiers, Battleships FROM Planets Where PlanetID = @param0 and SystemId = @param1",
                new List<object>() { planetId, systemId });
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    for (int i = 0; i < row.ItemArray.Length; i++)
                    {
                        if (int.Parse(row.ItemArray[i].ToString()) != 0) return true;
                    }
                }
            }
            return false;
        }
    };
}